package mas;

import java.util.Set;

import javax.annotation.Nullable;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;

import rinde.sim.core.graph.Point;
import rinde.sim.core.model.road.RoadModel;
import rinde.sim.ui.renderers.CanvasRenderer;
import rinde.sim.ui.renderers.UiSchema;
import rinde.sim.ui.renderers.ViewPort;
import rinde.sim.ui.renderers.ViewRect;

public class MyMessagingLayerRenderer implements CanvasRenderer {

	protected RoadModel rs;
	protected boolean useEncirclement;
	private final UiSchema uiSchema;

	public MyMessagingLayerRenderer(RoadModel rs, UiSchema uiSchema) {
		this.rs = rs;
		this.uiSchema = uiSchema;
	}

	public void renderDynamic(GC gc, ViewPort vp, long time) {
		final int size = 4;
		uiSchema.initialize(gc.getDevice());

		final Set<Truck> objects = rs
				.getObjectsOfType(Truck.class);

		synchronized (objects) {
			for (final Truck a : objects) {
				Point p = a.getPosition();
				if (p == null) {
					continue;
				}
				final int x = (int) (vp.origin.x + (p.x - vp.rect.min.x) * vp.scale);
				final int y = (int) (vp.origin.y + (p.y - vp.rect.min.y) * vp.scale);

				final int radius = (int) (a.getRadius() * vp.scale);

				Color c = null;
				if (a.getReliability() < 0.15) {
					c = uiSchema.getColor("color.black");
				} else if (a.getReliability() >= 0.15 && a.getReliability() < 0.3) {
					c = uiSchema.getColor("color.yellow");
				} else {
					c = uiSchema.getColor("color.green");
				}

				gc.drawText("r:" + a.getNoReceived(), x, y, true);

				final Set<Truck> communicatedWith = a.getCommunicatedWith();
				for (final Truck cw : communicatedWith) {
					p = cw.getPosition();
					if (p == null) {
						continue;
					}
					final int xCW = (int) (vp.origin.x + (p.x - vp.rect.min.x) * vp.scale);
					final int yCW = (int) (vp.origin.y + (p.y - vp.rect.min.y) * vp.scale);
					gc.drawLine(x, y, xCW, yCW);
				}
			}
		}
	}

	public void renderStatic(GC gc, ViewPort vp) {
	}

	public ViewRect getViewRect() {
		return null;
	}

}
